package org.hackystat.sdsa.analyzer.model.activity.refactoring;

import java.util.Map;
import java.util.logging.Logger;

import org.hackystat.sdsa.analyzer.control.SensorDataUtil;
import org.hackystat.sdsa.analyzer.model.activity.Activity;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;

/**
 * Implements action regarding to refactoring on java class, method etc.
 * 
 * @author Hongbing Kou
 */
public abstract class RefactorActivity extends Activity {
  /** Refactoring operator. */ 
  private RefactorOperator op;
  /** Refactoring subject type*/
  private RefactorSubjectType subjectType;

  /** Subsub type. */
  public static final String SUBSUB_TYPE = "subsubtype";
  /** Unit type. */
  public static final String UNIT_TYPE = "unit-type";

  /**
   * Empty constructor.
   * 
   * @param op RefactorOperator.
   * @param st SubjectType.
   */
  public RefactorActivity(RefactorOperator op, RefactorSubjectType st) {
    this.op = op;
    this.subjectType = st;
    
    super.setType(op.getName() + " " + subjectType.getName());
  }
  
  /**
   * Constructs refactoring activity.
   * 
   * @param sensorData Sensor data.
   * @param logger Service logger.
   */
  public RefactorActivity(SensorData sensorData, Logger logger) {
    super(sensorData, logger);
    
    Map<String, String> propMap = SensorDataUtil.propMap(sensorData);
    String op = propMap.get(SUBSUB_TYPE);
    // Perhaps sensor could not identify operation correctly all the time
    if (op == null) {
       op = "unknown"; 
    }
    this.op = RefactorOperator.getInstance(op);
    
    String unitType = propMap.get(UNIT_TYPE);
    // Perhaps sensor could not identify unit type correctly all the time
    if (unitType == null) { 
      unitType = "unknown";
    }
    this.subjectType = RefactorSubjectType.getInstance(unitType);
    super.setType(this.op.getName() + " " + subjectType.getName());
  }

  /**
   * Sets the refactor operator.
   * 
   * @param op Refactoring operator.
   */
  public void setOperator(RefactorOperator op) {
    this.op = op;     
    super.setType(op.getName() + " " + subjectType.getName());
  }
  
  /**
   * Gets refactoring operator. 
   * 
   * @return Refactoring operator.
   */
  public RefactorOperator getOperator() {
    return this.op;
  }

  /**
   * Sets refactor subject type.
   * 
   * @param subjectType Refactor subject type. 
   */
  public void setSubjectType(RefactorSubjectType subjectType) {
    this.subjectType = subjectType;   
    
    super.setType(this.op.getName() + " " + subjectType.getName());
  }
  
  
  /**
   * Gets the refactoring subject type.
   * 
   * @return Type of this refactor action on.
   */
  public RefactorSubjectType getSubjectType() {
    return this.subjectType;
  }
}